var app = getApp();
const FN = require('../../utils/public');
import commonAPI from '../../api/api';

Page({
  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    buttons: [{
      type: 'primary',
      className: 'goRepeatBtn',
      text: '去赚积分',
      buttontap: "",
      value: 1
    }],
    myintegral: 10,
    integralObj: {},
    myPageParams: {
      "learningDays": 0,
      "exerciseDays": 0,
      "arenaNum": 0,
    },
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    signInNum: [{
      signDay: 1
    }, {
      signDay: 2
    }, {
      signDay: 3
    }, {
      signDay: 4
    }, {
      signDay: 5
    }, {
      signDay: 6
    }, {
      signDay: 7
    }],
    signDay: 0,
    msgNum: 0,
    teamsgnum: 0,
    msgnoMember: 0,
    wxinfo: {},
    userType: '03',
    pointsconfigStr:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    let result = app.globalData.miniappInfo.pointsconfigList.map((item, index) => {
      let type = item.pointsConfigType === "1" ? "+" : "-";
      return `${index+1}、 ${item.pointsConfigName}${type}${item.pointsConfigValue}\n`;
  }).join('');
    setTimeout(() => {
      that.setData({
        wxinfo:app.globalData.userInfo.signInfo,
        pointsconfigStr:result
      })  
    }, 500);
    // that.wxInfoDiff();
  },

  // 跳转编辑头像昵称页面
  onSetNickName() {
    wx.navigateTo({
      url: '../editUserInfo/editUserInfo'
    })
  },

  setTeaInfo: function () {
    var that = this;
    // 01会员学生，02老师 03 普通游客
    that.setData({
      userType: app.globalData.userInfo.userType,
      msgNum: app.globalData.userInfo.msgNum
    })
  },

  // 关闭登录窗口 
  onCloseLoginBox: function (e) {
    var that = this;
    if (app.globalData.userInfo.integralObj) {
      this.setData({
        wxinfo: app.globalData.userInfo.signInfo,
      })
    }
    that.initData();
  },

  // 福利领取提示
  tipWord: function () {
    var that = this;
    that.setData({
      show: true
    })
  },

  // 去首页
  goRepeat: function () {
    wx.navigateTo({
      url: '../myIntegral/myIntegral'
    })
  },

  initData: function () {
    var that = this;
    // 初始化数据
    that.myStatisticsData(); // 学习天数 ,练习次数
    //that.signinNum(); // 签到次数
  },

  /**
   * 获取签到天数
   * 1. 如果dayDiffNum签到间隔天数超过1天就将连续签到归零
   */
  updateSignInData() {
    const { userInfo } = app.globalData;
    const { dayDiffNum, signInfo, integralObj } = userInfo;
    const num = dayDiffNum === 0 || dayDiffNum === 1 ? (signInfo?.hasOwnProperty('signInNum') ? signInfo.signInNum : 0) : 0;
  
    const arr = Array.from({ length: 7 }, (_, i) => {
      const n = num < 7 ? 0 : parseInt(num / 7);
      const signDay = n * 7 + i + 1;
      const signIcon = (i < num % 7 && num > 0) ? 1 : 0;
  
      return { signDay, signIcon };
    });
  
    this.setData({
      signInNum: arr,
      signDay: num,
      integralObj,
    });
  },
  //我的单词本
  mywordbook: function () {
    var that = this;
    wx.navigateTo({
      url: '../mywordbook/mywordbook'
    })
  },

  // 积分明细
  integralDetail: function () {
    wx.navigateTo({
      url: '../myIntegral/myIntegral'
    })
  },

  // 我的作业
  myhomework: function () {
    var that = this;
    wx.navigateTo({
      url: '../myhomework/myhomework'
    })
  },

  // 我的学习记录
  repeatRecords: function () {
    var that = this;
    wx.navigateTo({
      url: '../repeatRecords/repeatRecords'
    })
  },

  myStatisticsData: function () {
    var that = this;
    commonAPI.statisticsData({}).then(res => {
      var learningDays = 'myPageParams.learningDays';
      var exerciseDays = 'myPageParams.exerciseDays';
      var arenaNum = 'myPageParams.arenaNum';
      that.setData({
        [learningDays]: res.data[0].studyCount,// 学习天数
        [exerciseDays]: res.data[0].practiceCount,// 口语练习
        [arenaNum]: res.data[0].arenaNum//参与挑战数据
      })
    });
  },  

  // 纠音历史
  mynewcorrectmsg: function () {
    wx.navigateTo({
      url: '../admin4TeacherPage/newcorrectmsg/newcorrectmsg'
    })
  },

  // 我的纠音
  myjy: function () {
    wx.navigateTo({
      url: '../admin4TeacherPage/index4Teacher/index4Teacher'
    })
  },

  // 1对1纠音 预约纠音
  oneTooneCorrect: function () {
    wx.navigateTo({
      url: '../admin4TeacherPage/one2oneCorrect/one2oneCorrect?wdTeacherName=' + app.globalData.userInfo.userObj.wdTeacherName + '&wdTeacherId=' + app.globalData.userInfo.userObj.wdTeacherId
    })
  },

  // 纠音历史
  mynewcorrectmsg: function () {
    wx.navigateTo({
      url: '../admin4TeacherPage/newcorrectmsg/newcorrectmsg'
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    app.setPageTabBarBadge(); //实时更新主菜单我的消息数字
    app.callbackSignIntegralInfo = res=>{
      that.updateSignInData();// 获取签到天数
      that.initData();
      that.setTeaInfo(); // 学生或老师的消息数字  
      that.setData({
        wxinfo: res.userInfo.signInfo||res.userInfo.integralObj,
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})